import 'dart:convert';
import 'dart:math' as math;

import '../bean/BlockBean.dart';
import '../utils/TimeUtils.dart';
import '../wight/My.dart';
import 'package:json_annotation/json_annotation.dart';
import '../bean/GemBean.dart';

const maxWidth = 5; //随机横排最大个数，如果要增加横排个数，这里切记需要弄成一样
const maxHight = 5;

const BLOCK_SHAPES = [
  //一组\
  [
    [
      [1]
    ],
    [
      [1, 1],
      [1, 1]
    ]
  ],

  //二组
  [
    [
      //2
      [1, 1]
    ],
    [
      [1],
      [1],
    ],
    [
      [1, 1, 1]
    ],
    [
      [1],
      [1],
      [1]
    ],
    [
      [1, 1, 1, 1]
    ],
    [
      [1],
      [1],
      [1],
      [1]
    ],
    [
      [1, 1, 1, 1, 1]
    ],
    [
      [1],
      [1],
      [1],
      [1],
      [1]
    ],
  ],
//三组 10
  [
    [
      //11
      [1, 0],
      [1, 1],
      [0, 1],
    ],
    [
      [0, 1, 1],
      [1, 1, 0]
    ],
    [
      [0, 1],
      [1, 1],
      [1, 0]
    ],
    [
      [1, 1, 0],
      [0, 1, 1]
    ],
  ],

  //4组
  [
    [
      //14
      [0, 1],
      [0, 1],
      [1, 1]
    ],
    [
      [1, 1, 1],
      [0, 0, 1]
    ],
    [
      [1, 1],
      [1, 0],
      [1, 0],
      [1, 0]
    ],
    [
      [1, 0, 0],
      [1, 1, 1]
    ],
  ],
// 5组
  [
    [
      //18
      [1, 0],
      [1, 0],
      [1, 1]
    ],
    [
      [1, 1, 1],
      [1, 0, 0]
    ],
    [
      [1, 1],
      [0, 1],
      [0, 1]
    ],
    [
      [0, 0, 1],
      [1, 1, 1]
    ],
  ],
  //6组
  [
    [
      //22
      [1, 0],
      [1, 1],
      [1, 0]
    ],
    [
      [1, 1, 1],
      [0, 1, 0]
    ],
    [
      [0, 1],
      [1, 1],
      [0, 1]
    ],
    [
      [0, 1, 0],
      [1, 1, 1]
    ],
  ],
  //7组
  [
    [
      //26
      [1, 0],
      [0, 1],
    ],
    [
      [1, 0, 0],
      [0, 1, 0],
      [0, 0, 1]
    ],
    [
      [0, 1],
      [1, 0],
    ],
    [
      [0, 0, 1],
      [0, 1, 0],
      [1, 0, 0]
    ],
  ],
  //8组
  [
    [
      //30
      [1, 0, 1],
      [1, 1, 1]
    ],
    [
      [1, 1],
      [1, 0],
      [1, 1]
    ],
    [
      [1, 1, 1],
      [1, 0, 1]
    ],
    [
      [1, 1],
      [0, 1],
      [1, 1]
    ],
  ],
  //9组

  [
    [
      //34
      [0, 1],
      [1, 1]
    ],
    [
      [1, 0],
      [1, 1]
    ],
    [
      [1, 1],
      [1, 0]
    ],
    [
      [1, 1],
      [0, 1]
    ],
  ],

  //10组
  [
    [
      //38
      [1, 0, 0],
      [1, 1, 1],
      [1, 0, 0]
    ],
    [
      [1, 1, 1],
      [0, 1, 0],
      [0, 1, 0]
    ],
    [
      [0, 0, 1],
      [1, 1, 1],
      [0, 0, 1]
    ],
    [
      [0, 1, 0],
      [0, 1, 0],
      [1, 1, 1]
    ],
  ]
];

//方框图片，
// const _IMsSd = [
//   'assets/images/square.png',
//   'assets/images/square.png',
// ];

const int ronudCOunt = 3; //随机方块的数量，下面写死的，如果需要活可以用for循环即可。

// class Block2 {
//    BlockType type=BlockType.D;

//    Map<String, dynamic> toJson() {
//     final Map<String, dynamic> data = new Map<String, dynamic>();
//       data['type'] = json.encode(type.index);
//     return data;
//  }
// }

@JsonSerializable()
class Block {
  // BlockType type = BlockType.D;
  int typeIndex = 0; //枚举的索引 ,只有通过这种方式才能存和取
  List<List<BlockBean>> shape = [];
  int rotateIndex = 0;
  int drag = 0; //0表示允许拖动，其他表示不允许拖动时，证明无法填充到棋盘

  Block(this.shape, this.rotateIndex, this.typeIndex);
  Block.fromJson(Map<String, dynamic> jsons) {
    typeIndex = jsons["typeIndex"];
    drag = jsons["drag"];
    rotateIndex = jsons['rotateIndex'];
    if (jsons['shape'] != null) {
      shape = [];
      List list = json.decode(jsons['shape']);
      for (var item2 in list) {
        List<BlockBean> d = (item2 as List<dynamic>)
            .map((e) => BlockBean.fromJson(e as Map<String, dynamic>))
            .toList();
        shape.add(d);
      }
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['typeIndex'] = this.typeIndex;
    data['rotateIndex'] = this.rotateIndex;
    data["drag"] = this.drag;
    data['shape'] = jsonEncode(this.shape);

    return data;
  }

  //获取三个随机木块   type 1经典， 2挑战 3旅行

  static List<Block> getRandom(int types,
      {List<GemBean>? gemList, bool isgem = false}) {
    List<Block> list = [];
    final i = math.Random().nextInt(BLOCK_SHAPES.length);
    int gemPosont = -2; //挑战模式/旅行模式，宝石随机赋值在三个的其中一个
    if (types == 2 || types == 3) {
      gemPosont = math.Random().nextInt(ronudCOunt);
    }

    final i11 = math.Random().nextInt(BLOCK_SHAPES[i].length);
    bool isadd = false;
    List<List<int>>? shape = BLOCK_SHAPES[i][i11];
    // print("随机1:" + i.toString());
    int x = -1;
    int y = -1;
    List<List<BlockBean>> initialRandom = [];
    for (int i = 0; i < shape.length; i++) {
      List<BlockBean> newList = [];
      for (int j = 0; j < shape[i].length; j++) {
        BlockBean bean = BlockBean();
        bean.normal = shape[i][j];
        if (shape[i][j] == 1) {
          x = i;
          y = j;
          if (isadd) {
            bean.img = "assets/images/square.png";
          } else {
            var getGem = _getGem(gemPosont, 0);
            if (getGem) {
              isadd = true;
              if (types == 2) {
                bean.gem = 1;
                bean.img = evearyGem[TimeUtils.CurrentMonth()];
              } else if (types == 3 && isgem) {
                var nextInt = math.Random().nextInt(gemList!.length);
                bean.img = gemList[nextInt].img;
                //    print("随机第一个宝石种类索引${nextInt}");
                bean.gem = nextInt + 1; //gemList的索引加1
              } else {
                bean.img = "assets/images/square.png";
              }
            } else {
              bean.img = "assets/images/square.png";
            }
          }
        }
        newList.add(bean);
      }
      initialRandom.add(newList);
    }
    if (gemPosont == 0 && !isadd) {
      //还没添加宝石，就默认最后一个添加宝石
      if (types == 2) {
        initialRandom[x][y].normal = 1;
        initialRandom[x][y].gem = 1;
        initialRandom[x][y].img = evearyGem[TimeUtils.CurrentMonth()];
      } else if (types == 3 && isgem) {
        var nextInt = math.Random().nextInt(gemList!.length);
        initialRandom[x][y].img = gemList[nextInt].img;
        initialRandom[x][y].normal = 1;
        initialRandom[x][y].gem = nextInt + 1;
      }
    }

    list.add(Block(initialRandom, 0, i));

    int i2 = math.Random().nextInt(BLOCK_SHAPES.length);
    if (i2 == i) {
      while (true) {
        i2 = math.Random().nextInt(BLOCK_SHAPES.length);
        if (i2 != i) {
          break;
        }
      }
    }

    final i22 = math.Random().nextInt(BLOCK_SHAPES[i2].length);
    //获取第一个的
    // print("随机2:" + i2.toString());
    final shape2 = BLOCK_SHAPES[i2][i22];

    List<List<BlockBean>> initialRandom2 = [];
    for (int i = 0; i < shape2.length; i++) {
      List<BlockBean> newList = [];
      for (int j = 0; j < shape2[i].length; j++) {
        BlockBean bean = BlockBean();
        bean.normal = shape2[i][j];
        if (shape2[i][j] == 1) {
          x = i;
          y = j;
          if (isadd) {
            bean.img = "assets/images/square.png";
          } else {
            var getGem = _getGem(gemPosont, 1);
            if (getGem) {
              isadd = true;

              if (types == 2) {
                bean.gem = 1;
                bean.img = evearyGem[TimeUtils.CurrentMonth()];
              } else if (types == 3 && isgem) {
                var nextInt = math.Random().nextInt(gemList!.length);
                bean.img = gemList[nextInt].img;
                //   print("随机第2个宝石种类索引${nextInt}");
                bean.gem = nextInt + 1;
              } else {
                bean.img = "assets/images/square.png";
              }
            } else {
              bean.img = "assets/images/square.png";
            }
          }
        }
        newList.add(bean);
      }
      initialRandom2.add(newList);
    }

    if (gemPosont == 1 && !isadd) {
      //还没添加宝石，就默认最后一个添加宝石

      if (types == 2) {
        initialRandom2[x][y].normal = 1;
        initialRandom2[x][y].gem = 1;
        initialRandom2[x][y].img = evearyGem[TimeUtils.CurrentMonth()];
      } else if (types == 3 && isgem) {
        var nextInt = math.Random().nextInt(gemList!.length);
        initialRandom2[x][y].img = gemList[nextInt].img;
        initialRandom2[x][y].gem = nextInt + 1;
        initialRandom2[x][y].normal = 1;
      }
    }

    list.add(Block(initialRandom2, 0, i2));

    int i3 = math.Random().nextInt(BLOCK_SHAPES.length);
    if (i3 == i || i2 == i3) {
      while (true) {
        i3 = math.Random().nextInt(BLOCK_SHAPES.length);
        if (i3 != i && i3 != i2) {
          break;
        }
      }
    }

    final i33 = math.Random().nextInt(BLOCK_SHAPES[i3].length);
    //获取第一个的
    // print("随机2:" + i2.toString());
    final shape3 = BLOCK_SHAPES[i3][i33];

    List<List<BlockBean>> initialRandom3 = [];
    for (int i = 0; i < shape3.length; i++) {
      List<BlockBean> newList = [];
      for (int j = 0; j < shape3[i].length; j++) {
        BlockBean bean = BlockBean();
        bean.normal = shape3[i][j];
        if (shape3[i][j] == 1) {
          x = i;
          y = j;
          if (isadd) {
            bean.img = "assets/images/square.png";
          } else {
            var getGem = _getGem(gemPosont, 2);
            if (getGem) {
              isadd = true;
              if (types == 2) {
                bean.gem = 1;
                bean.img = evearyGem[TimeUtils.CurrentMonth()];
              } else if (types == 3 && isgem) {
                var nextInt = math.Random().nextInt(gemList!.length);
                bean.img = gemList[nextInt].img;
                bean.gem = nextInt + 1;
                //  print("随机第3个宝石种类索引${nextInt}");
              } else {
                bean.img = "assets/images/square.png";
              }
            } else {
              bean.img = "assets/images/square.png";
            }
          }
        }
        newList.add(bean);
      }
      initialRandom3.add(newList);
    }

    if (gemPosont == 2 && !isadd) {
      //还没添加宝石，就默认最后一个添加宝石
      if (types == 2) {
        initialRandom3[x][y].gem = 1;

        initialRandom3[x][y].img = evearyGem[TimeUtils.CurrentMonth()];
      } else if (types == 3 && isgem) {
        var nextInt = math.Random().nextInt(gemList!.length);
        initialRandom3[x][y].img = gemList[nextInt].img;
        initialRandom3[x][y].gem = nextInt + 1;
        initialRandom3[x][y].normal = 1;
      }
    }

    list.add(Block(initialRandom3, 0, i3));

    return list;
  }

/**
 * 是否需要宝石的显示 true需要
 */
  static bool _getGem(int gemPosont, int type) {
    if (gemPosont == type) {
      var nextDouble = math.Random().nextBool();
      if (nextDouble) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }
}
